package com.example.enterprisemanager.mvvm.fragment.manager;

import android.app.Application;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.enterprisemanager.base.BaseModel;
import com.example.enterprisemanager.base.BaseViewModel;
import com.example.enterprisemanager.binding.BindingCommand;
import com.example.enterprisemanager.mvvm.adapter.RuinAdapter;
import com.example.enterprisemanager.mvvm.ben.User;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.AllocateGoods;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.Goods;
import com.example.enterprisemanager.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobUser;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class RuinViewModel extends BaseViewModel<RuinModel> {
    public RuinAdapter adapter;
    public BindingCommand findCommand;
    public ObservableField<String> edtContent;

    public RuinViewModel(@NonNull Application application) {
        super(application, new RuinModel());
        init();
    }

    private void init() {
        edtContent = new ObservableField<>();

        adapter = new RuinAdapter();

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (!BmobUser.isLogin() || BmobUser.getCurrentUser(User.class).getPower() > 2) {
                    ToastUtil.show(getApplication(), "您的权限不足，需要的权限为2及以上");
                    return;
                }

                Goods good = (Goods) adapter.getItem(position);
                if (good == null) {
                    return;
                }

                mModel.updateAllocateId(good, (goods)->{
                    adapter.notifyItemChanged(position, goods);
                    ToastUtil.show(getApplication(), "报废登记成功");
                }, (e)->{
                });}});

        findCommand = new BindingCommand(() -> {
            String id = edtContent.get();
            if (id == null || id.isEmpty() || id.length() < 5) {
                ToastUtil.show(getApplication(), "输入内容有误，请检查后重新输入");
                return;
            }
            mModel.getGoods(id, (good)->{
                List<Goods> goods = new ArrayList<>();
                goods.add(good);
                adapter.setNewData(goods);
            }, (e)->{
                ToastUtil.show(getApplication(), "输入id错误，尚未正确查询到相关物品");
            }); });}


}
